package com.notepad.ui;

import java.util.ArrayList;
import java.util.List;

import com.notepad.R;
import com.notepad.adapter.ViewPagerAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SplashPager extends Activity {
	private ViewPager mViewPager;
	private ImageButton mImageButton;
	private LinearLayout mDotsLayout;
	private List<View> mViewList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_viewpager);
		init();
		initPager();
		mViewPager.setAdapter(new ViewPagerAdapter(mViewList));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				for (int i = 0; i < mDotsLayout.getChildCount(); i++) {
					if (i == arg0) {
						mDotsLayout.getChildAt(i).setSelected(true);
					} else {
						mDotsLayout.getChildAt(i).setSelected(false);
					}
					if (arg0 == mDotsLayout.getChildCount() - 1) {
						mImageButton.setVisibility(View.VISIBLE);
					} else {
						mImageButton.setVisibility(View.GONE);
					}
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		mImageButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				openHome();
			}
		});
	}

	private void init() {
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mImageButton = (ImageButton) findViewById(R.id.guide_btn);
		mDotsLayout = (LinearLayout) findViewById(R.id.guide_dots);
	}

	private View initDot() {
		return LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.layout_dot, null);
	}

	private View initView(int res, int text) {
		View view = LayoutInflater.from(this)
				.inflate(R.layout.guide_item, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.guide_img);
		ImageView textView = (ImageView) view.findViewById(R.id.guide_text);
		imageView.setImageResource(res);
		textView.setImageResource(text);
		return view;
	}

	
	private void initPager() {
		mViewList = new ArrayList<View>();
		int[] imgs = new int[] { R.drawable.new01, R.drawable.new02,
				R.drawable.new03, R.drawable.new04 };
		int[] text = new int[] { R.string.guide1, R.string.guide2,
				R.string.guide3, R.string.guide4 };
		for (int i = 0; i < imgs.length; i++) {
			mViewList.add(initView(imgs[i], text[i]));
		}
		initDots(imgs.length);
	}

	
	private void initDots(int count) {
		for (int j = 0; j < count; j++) {
			mDotsLayout.addView(initDot());
		}
		mDotsLayout.getChildAt(0).setSelected(true);
	}

	private void openHome() {
		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		startActivity(intent);
		finish();
	}

}
